using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WozLib.UI.Ink.SketchProperties;

namespace WozLib.UI.Controls
{
  /// <summary>
  /// Interaction logic for FontSelector.xaml
  /// </summary>

  public class FontString
  {

    public string Description
    {
      get { return _Description; }
      set { _Description = value; }
    } string _Description = "Verdana";

    public FontFamily Font
    {
      get { return new FontFamily(Description); }
      set { Description = value.ToString(); }
    }

    public FontString()
    {
    }

    public FontString(string description)
    {
      Description = description;
    }

    public override string ToString()
    {
      return Description;
    }
  }

  public partial class FontSelector : UserControl
  {
    public static readonly DependencyProperty SelectedFontProperty =
            DependencyProperty.Register("SelectedFont", typeof(string), typeof(FontSelector),
      new UIPropertyMetadata("", new PropertyChangedCallback(SelectedFont_Changed)));

    public string SelectedFont
    {
      get { return (string)GetValue(SelectedFontProperty); }
      set { SetValue(SelectedFontProperty, value); }
    }

    public static readonly DependencyProperty SelectedFontStringProperty =
            DependencyProperty.Register("SelectedFontString", typeof(string), typeof(FontSelector),
      new UIPropertyMetadata("", new PropertyChangedCallback(SelectedFontString_Changed)));

    public string SelectedFontString
    {
      get { return (string)GetValue(SelectedFontStringProperty); }
      set { SetValue(SelectedFontStringProperty, value); }
    }

    public static readonly DependencyProperty CascadingPropertyProperty =
            DependencyProperty.Register("CascadingProperty", typeof(CascadingProperty), typeof(FontSelector));

    public CascadingProperty CascadingProperty
    {
      get { return (CascadingProperty)GetValue(CascadingPropertyProperty); }
      set { SetValue(CascadingPropertyProperty, value); }
    }


    public FontSelector()
    {
      InitializeComponent();
      this.Loaded += new RoutedEventHandler(FontSelector_Loaded);
    }

    void FontSelector_Loaded(object sender, RoutedEventArgs e)
    {

      Binding valueBinding = new Binding();
      valueBinding.Mode = BindingMode.TwoWay;
      valueBinding.Source = this;
      valueBinding.Path = new PropertyPath(SelectedFontProperty);
      BindingOperations.SetBinding(_FontCombo, ComboBox.SelectedItemProperty, valueBinding);

      //_FontCombo.Items.Add(new FontString("Arial"));
      //_FontCombo.Items.Add(new FontString("Times New Roman"));
      //_FontCombo.Items.Add(new FontString("Trebuchet"));
      //_FontCombo.Items.Add(new FontString("Verdana"));
      //_FontCombo.Items.Add(new FontString("Courier New"));

      _FontCombo.Items.Add("Arial");
      _FontCombo.Items.Add("Times New Roman");
      _FontCombo.Items.Add("Trebuchet");
      _FontCombo.Items.Add("Verdana");
      _FontCombo.Items.Add("Courier New");
    }

    private static void SelectedFont_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      //if (d is FontSelector) {
      //  if ((d as FontSelector)._FontCombo.SelectedItem != null)
      //  {
      //    (d as FontSelector).SelectedFont = ((d as FontSelector)._FontCombo.SelectedItem as FontString).Description;
      //  }
      //  else
      //  {
      //    (d as FontSelector).SelectedFont = "Verdana";
      //  }
      //}
    }

    private static void SelectedFontString_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (d is FontSelector)
      {
        (d as FontSelector)._FontCombo.SelectedItem = new FontString((d as FontSelector).SelectedFont);
      }
    }
  }
}